package com.qianguo.sender.order;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.widget.NoScrollListView;
import com.qianguo.sender.R;
import com.qianguo.sender.api.entity.CatchOrder;
import com.qianguo.sender.api.entity.SenderOrderInfo;
import com.qianguo.sender.api.entity.ProductInfo;
import com.qianguo.sender.base.BaseTitleActivity;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 订单详情页
 *
 * @author eva
 * @date 16/5/5
 */
@EActivity(R.layout.activity_sender_order_detail)
public class SenderOrderDetailActivity extends BaseTitleActivity {

    @ViewById(R.id.sender_order_detail_buyer_name_tv)
    TextView mTvBuyerName;
    @ViewById(R.id.sender_order_detail_buyer_mobile_tv)
    TextView mTvBuyerMobile;
    @ViewById(R.id.sender_order_detail_delivery_date_tv)
    TextView mTvDeliveryDate;
    @ViewById(R.id.sender_order_detail_delivery_time_tv)
    TextView mTvDeliveryTime;
    @ViewById(R.id.sender_order_detail_comment_tv)
    TextView mTvComment;
    @ViewById(R.id.sender_order_detail_product_price_tv)
    TextView mTvProductPrice;
    @ViewById(R.id.sender_order_detail_delivery_price_tv)
    TextView mTvDeliveryPrice;
    @ViewById(R.id.sender_order_detail_catch_order_button)
    Button mBtnAction;


    @ViewById(R.id.sender_order_detail_product_list_view)
    NoScrollListView mProductListView;

    @Extra("order_id")
    String mOrderId;

    private SenderOrderInfo mOrder;

    private ProductListAdapter mAdapter;

    public static void jumpToOrderDetail(Context context, String orderId) {
        Intent intent = new Intent();
        intent.setClass(context, SenderOrderDetailActivity_.class);
        intent.putExtra("order_id", orderId);
        context.startActivity(intent);
    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.sender_order_detail_title);
    }

    @Override
    protected void initializeUI() {
        mAdapter = new ProductListAdapter(this);
        mProductListView.setAdapter(mAdapter);

        getOrderDetail(mOrderId);
    }

    @Background
    void getOrderDetail(String id) {
        mApp.getApi().getOrderDetail(mApp.getUser().getToken(), mOrderId, new HttpRequestCallback<SenderOrderInfo>() {
            @Override
            public void onSuccess(int code, String msg, SenderOrderInfo data) {
                mOrder = data;
                refresh(data);
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(SenderOrderDetailActivity.this, msg);
            }
        });

    }

    @UiThread
    void refresh(SenderOrderInfo order) {
        mTvBuyerName.setText(order.getReceiverName());
        mTvBuyerMobile.setText(order.getReceiverPhone());
        mTvComment.setText(order.getReceiverIntro());
        mTvDeliveryDate.setText(order.getDeliveryTime());
        mTvDeliveryPrice.setText(Integer.toString(order.getDeliveryPrice()));
        mTvDeliveryTime.setText(order.getDeliveryTime());
        mTvProductPrice.setText(Integer.toString(order.getPrice()));
        switch (order.getSendStatus()) {
            case SenderOrderInfo.SEND_STATUS_UNDO:
                mBtnAction.setText(getString(R.string.sender_tab_catch_order));
                mBtnAction.setOnClickListener(mCatchOrderListener);
                break;
            case SenderOrderInfo.SEND_STATUS_DOING:
                mBtnAction.setText(getString(R.string.sender_order_action_confirm_arrived));
                mBtnAction.setOnClickListener(mConfirmReceiverListener);
                break;
            case SenderOrderInfo.SEND_STATUS_FINISH:
                mBtnAction.setVisibility(View.GONE);
                break;
        }
//        mAdapter.notifyDataChange(order.getProducts());
    }

    private View.OnClickListener mConfirmReceiverListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            confirm();
        }
    };

    private View.OnClickListener mCatchOrderListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            catchOrder();
        }
    };

    @Background
    void confirm() {
        mApp.getApi().confirmReceive(mApp.getUser().getToken(), mOrderId, new HttpRequestCallback<SenderOrderInfo>() {
            @Override
            public void onSuccess(int code, String msg, SenderOrderInfo data) {
                finish();
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(SenderOrderDetailActivity.this, msg);
            }
        });
    }

    @Background
    void catchOrder() {
        mApp.getApi().catchOrder(mApp.getUser().getToken(), mOrderId, new HttpRequestCallback<CatchOrder>() {
            @Override
            public void onSuccess(int code, String msg, CatchOrder data) {
                finish();
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(SenderOrderDetailActivity.this, msg);
            }
        });
    }

    private class ProductListAdapter extends BaseAdapter {

        private Context mContext;
        private List<ProductInfo> mProducts = new ArrayList<>();

        public ProductListAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return mProducts == null ? 0 : mProducts.size();
        }

        @Override
        public Object getItem(int i) {
            return mProducts.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                view = LayoutInflater.from(mContext).inflate(R.layout.item_order_detail_product_list, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.onBind((ProductInfo) getItem(i));
            return view;
        }

        private class ViewHolder {
            TextView tvProductName;
            TextView tvProductPrice;

            public ViewHolder(View itemView) {
                tvProductName = (TextView) itemView.findViewById(R.id.item_order_detail_product_name_tv);
                tvProductPrice = (TextView) itemView.findViewById(R.id.item_order_detail_product_price_tv);
            }

            public void onBind(ProductInfo product) {
                tvProductName.setText(product.getName());
                tvProductPrice.setText(product.getPrice());
            }
        }
    }
}
